﻿using System;
using System.Windows;
using System.Windows.Input;
using Gencode.VECore.VectorShape;
using Gencode.VECore.Tools;

namespace Gencode.VECore.Tools
{
    public class PolygonTool : FactoryTool
    {
        VECPolygon curentPath;

        protected override VECShape CreateShape(Point mousePosition)
        {
            curentPath = new VECPolygon(mousePosition);

            curentPath.StrokeThickness = ToolProperty.StrokeThickness;
            curentPath.Stroke = ToolProperty.Stroke;

            return curentPath;
        }

        public override void OnMouseLeftButtonDown(CanvasDocument sender, MouseButtonEventArgs e)
        {
            //create new object if none exists
            if (curentPath == null)
            {
                sender.AddShape(CreateShape(Mouse.GetPosition(sender)));
                return;
            }

            //add new path to curentpath
            curentPath.AddSegment(Mouse.GetPosition(sender));
        }

        public override void OnMouseLeftButtonUp(CanvasDocument sender, MouseButtonEventArgs e)
        {
        }

        public override void OnMouseMove(CanvasDocument sender, MouseEventArgs e)
        {
            if (curentPath != null)
            {
                Point m = Mouse.GetPosition(sender);
                if (curentPath != null)
                    curentPath.MoveHandle(m, curentPath.MaxHandle);
            }
        }

        public override void OnMouseRightButtonUp(CanvasDocument sender, MouseButtonEventArgs e)
        {
            //Add object to history
            sender.AddCommandToHistory(new Gencode.VECore.History.AddCommand(curentPath));

            //end of object
            curentPath = null;
        }
    }
}
